package main

import (
	"fmt"
	"os"

	"gopkg.in/yaml.v2"
)

type PrometheusConfig struct {
	JobName       string         `yaml:"job_name"`
	StaticConfigs []StaticConfig `yaml:"static_configs"`
}

type StaticConfig struct {
	Targets []string `yaml:"targets"`
}

func main() {
	// 你的输入数据
	data := []map[string]string{
		{"name": "service1", "addr": "localhost:8080"},
		{"name": "service2", "addr": "localhost:8081"},
		{"name": "service3", "addr": "localhost:8082"},
	}

	// 将数据转换为 Prometheus 配置结构
	configs := make([]PrometheusConfig, 0)

	for _, item := range data {
		config := PrometheusConfig{
			JobName: item["name"],
			StaticConfigs: []StaticConfig{
				{
					Targets: []string{item["addr"]},
				},
			},
		}
		configs = append(configs, config)
	}

	// 生成 YAML
	yamlData, err := yaml.Marshal(configs)
	if err != nil {
		fmt.Printf("Error marshaling YAML: %v\n", err)
		return
	}

	// 输出到文件或控制台
	fmt.Println("Generated YAML:")
	fmt.Println(string(yamlData))

	// 如果需要写入文件
	err = os.WriteFile("prometheus.yml", yamlData, 0644)
	if err != nil {
		fmt.Printf("Error writing file: %v\n", err)
		return
	}
}
